SPB Git

Repository Public

route.ts 2.0 KB 32 lines typescript

1// Vrai-Prix — coût par la méthode du coût d'une annonce analysée : lecture (GET) et recalcul aux coûts d'aujourd'hui (POST) — sans rappeler le modèle.2import { NextRequest, NextResponse } from "next/server";3import { AnalysisError, getById, getLatest, recalc } from "@/lib/cost/ai/service";4import { clientKey, rateLimit } from "@/lib/cost/cache";56export const runtime = "nodejs";78export async function GET(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {9  const { uid } = await params;10  const id = req.nextUrl.searchParams.get("id");11  const v = id ? getById(id) : getLatest(decodeURIComponent(uid));12  if (!v || v.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "aucune analyse" }, { status: 404 });13  if (!v.estimate) return NextResponse.json({ error: "aucune estimation (analyse en cours ou échouée)", status: v.status }, { status: 409 });14  return NextResponse.json({ analysisId: v.id, costSnapshotDate: v.costSnapshotDate, combined: v.combined, estimate: v.estimate }, { headers: { "Cache-Control": "no-store" } });15}1617export async function POST(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {18  const { uid } = await params;19  if (!rateLimit(`ai-recalc:${clientKey(req)}`, 20, 60_000)) return NextResponse.json({ error: "Trop de requêtes." }, { status: 429 });20  let body: { analysisId?: string } = {};21  try { body = await req.json(); } catch { /* corps vide permis */ }22  try {23    const cur = body.analysisId ? getById(body.analysisId) : getLatest(decodeURIComponent(uid));24    if (!cur || cur.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "aucune analyse" }, { status: 404 });25    const v = recalc(cur.id);26    return NextResponse.json(v, { headers: { "Cache-Control": "no-store" } });27  } catch (e) {28    if (e instanceof AnalysisError) return NextResponse.json({ error: e.message, code: e.code }, { status: e.status });29    return NextResponse.json({ error: (e as Error).message }, { status: 500 });30  }31}32
Files main
Menu

Theme